import { getCountInfoAPI } from "../../api/debtor";
import { debtorPrice, zcc } from "../../utils/util";
import { getMessagePageAPI } from "../../api/monitor";
import { formatDate } from "../../utils/util";
import { IAppOption } from "../../../typings";
import { ZCC } from "../../utils/BasePage";
import { off } from "process";
import { hideModuleConfigAPI } from "../../api/open";
new ZCC.BasePage({
  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    safeNavHeight: 91,
    fsVisible: false, // 展示定位搜索栏
    listLoading: false,
    infoLoading: false,
    refreshLoading: false,
    previewList: [
      {
        num: "0",
        unit: "个",
        name: "债务人",
        iconPath: "preview_debtor",
        aclass: "actived",
        path: "/subPackages/packageA/debtorList/debtorList",
      },
      {
        num: "0",
        unit: "条",
        name: "财产线索",
        iconPath: "preview_clue",
        aclass: "",
        path: "/pages/propertyClue/propertyClue",
      },
      {
        num: "0",
        unit: "个",
        name: "担保人",
        iconPath: "preview_bondsman",
        aclass: "",
        path: "/subPackages/packageA/debtorList/debtorList",
      },
      {
        num: "0",
        unit: "万元",
        name: "财产线索价值",
        iconPath: "preview_value",
        aclass: "",
        path: "/pages/propertyClue/propertyClue",
      },
    ],
    monitorList: [],
    // 返回顶部
    isShow: false,
    companyName: "",
    page: 1,
    perPage: 10,
    total: 0,
    navBGOpacity: 0.0,
    hideShiye: true,
  },
  async hideModule() {
    const res: {
      code: string;
      content: string[];
      desc: string;
      timestamp: number;
    } = await hideModuleConfigAPI("app");
    if (
      res.code === "000" &&
      res.content &&
      res.content.length > 0 &&
      res.content.includes("tabShiYe")
    ) {
      this.setData({
        hideShiye: true,
      });
    } else {
      this.setData({
        hideShiye: false,
      });
    }
  },
  // 点击banner1  跳转到师爷大模型页面
  skipAiPage() {
    wx.switchTab({
      url: "/pages/shiye/shiye",
    });
  },
  // 企业概览 点击跳转
  skipHandle(e: any) {
    let index = e.currentTarget.dataset.index;
    let newArray = this.data.previewList.slice();
    let iconPath = newArray[index].iconPath;
    let path = e.currentTarget.dataset.path;
    if (index < newArray.length) {
      newArray.forEach((element: any, idx: any) => {
        element.aclass = index === idx ? "actived" : "";
      });
    }
    this.setData({
      previewList: newArray,
    });
    // 跳转
    if (iconPath === "preview_clue" || iconPath === "preview_value") {
      getApp().globalData.prevPage = "index";
      wx.switchTab({
        url: path,
      });
    } else {
      wx.navigateTo({
        url: path,
      });
    }
  },
  backTop() {
    this.setData({
      scrollTop: 0
    })
  },
  // onPageScroll(e) {
  //   let top = e.scrollTop;
  //   console.log(top, "top")
  //   let isShow = top > 10;
  //   this.setData({
  //     isShow: isShow,
  //   });
  // },
  // 获取企业概览数据
  getCountInfo() {
    this.data.infoLoading = true;
    return getCountInfoAPI().then((res: any) => {
      if (res.code === "000") {
        let content = res.content;
        let newArray = this.data.previewList.slice();
        newArray[0]["num"] = content.debtorNum;
        newArray[1]["num"] = content.clueNum;
        newArray[2]["num"] = content.assureNum;
        newArray[3]["num"] = debtorPrice(content.clueAmount);
        this.setData({
          previewList: newArray,
        });
        this.data.infoLoading = false;
      }
    });
  },
  // 近30天消息监控列表
  getMonitorList(resetFlag?: boolean) {
    this.setData({
      listLoading: true,
    });
    let para = {
      page: this.data.page,
      perPage: this.data.perPage,
      startTime: formatDate(Date.now() - 30 * 24 * 3600 * 1000),
      endTime: formatDate(Date.now()),
    };
    return getMessagePageAPI(para).then((res: any) => {
      if (res.code === "000") {
        if (resetFlag) {
          this.reset();
        }
        let content = res.content;
        let contentList = res.content.list || [];
        contentList.forEach((item: any) => {
          if (item.message) {
            item.message = JSON.parse(item.message);
          }
        });
        this.data.total = content.total;
        let monitorList = this.data.monitorList;
        this.setData({
          monitorList: monitorList.concat(contentList),
          listLoading: false,
        });
      }
    });
  },
  // 列表滾到到底
  scrollToLowerHandle() {
    if (this.data.total > this.data.monitorList.length) {
      this.data.page++;
      this.getMonitorList();
    }
  },
  // 数据重置
  reset() {
    this.data.page = 1;
    this.setData({
      monitorList: [],
    });
  },
  // 下拉刷新
  async bindrefresherrefresh() {
    this.setData({
      refreshLoading: true,
      navBGOpacity: 0.0,
    });
    await this.getCountInfo();
    await this.getMonitorList(true);
    this.setData({
      refreshLoading: false,
    });
  },
  // 点击搜索
  searchDebtorPage() {
    wx.navigateTo({
      url: "/subPackages/packageA/debtorSearch/debtorSearch",
    });
  },
  // 滚动监听，定位搜索栏
  scrollWatch(e: any) {
    const { scrollTop, deltaY } = e.detail;
    // console.log("onScroll : ", e);
    this.setData({
      isShow: scrollTop > 400,
    });
    const op = zcc.getNavOpacityWhenScroll({
      initOpacity: this.data.navBGOpacity,
      scrollTop: scrollTop,
      deltaY: deltaY,
    });
    if (op != this.data.navBGOpacity) {
      this.setData({
        navBGOpacity: op,
      });
    }

    if (scrollTop > 115) {
      this.setData({
        fsVisible: true,
      });
    } else {
      this.setData({
        fsVisible: false,
      });
    }
  },
  // getTabBar 回调设置自定义tab-bar 高亮
  getInstance() {
    if (typeof this.getTabBar === "function") {
      this.getTabBar((tabBar: any) => {
        tabBar.setData({
          active: 0,
        });
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.hideModule();
    wx.getSystemInfo({
      success: (res) => {
        const isAndroid = res.platform === "android";
        let safeNavHeight =
          (isAndroid ? 48 : 44) +
          (res.safeArea.top > 0 ? res.safeArea.top : 35);
        // wx.showModal({
        //   title: res.safeArea.top + '/'+ res.platform
        // })
        this.setData({
          safeNavHeight,
        });
      },
    });
    this.getMonitorList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInstance();
    const islogin = getApp<IAppOption>().globalData.isLogin;
    if (islogin) {
      this.getCountInfo();
      // this.getMonitorList()
    } else {
      wx.redirectTo({
        url: "/pages/login/login",
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },
});
